from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.contrib import admin


class Platform(models.Model):
    rank = models.SmallIntegerField(_('rank'))
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = _('platform')
        verbose_name_plural = _('platforms')
        ordering = ["rank"]
        
class PlatformAdmin(admin.ModelAdmin):
            list_display = ['rank' , 'name']
            
admin.site.register(Platform, PlatformAdmin)

class TicketCategory(models.Model):
    rank = models.SmallIntegerField(_('rank'))
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = _('ticket category')
        verbose_name_plural = _('ticket categories')
        ordering = ["rank"]
        
class TicketCategoryAdmin(admin.ModelAdmin):
            list_display = ['rank' , 'name']
            
admin.site.register(TicketCategory, TicketCategoryAdmin)

class Priority(models.Model):
    rank = models.SmallIntegerField(_('rank'))
    name = models.CharField(_('name'), max_length=30)
    response_time = models.IntegerField(_('response time'))
    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = _('priority')
        verbose_name_plural = _('priorities')
        ordering = ["rank"]
        
class PriorityAdmin(admin.ModelAdmin):
            list_display = ['rank' , 'name']
            
admin.site.register(Priority, PriorityAdmin)


class Rating(models.Model):
    rank = models.SmallIntegerField(_('rank'))
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["rank"]
        verbose_name = _('rating')
        verbose_name_plural = _('ratings')
        
class RatingAdmin(admin.ModelAdmin):
            list_display = ['rank' , 'name']
            
admin.site.register(Rating, RatingAdmin)

class Status(models.Model):
    rank = models.SmallIntegerField(_('rank'))
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["rank"]
        verbose_name = _('status')
        verbose_name_plural = _('statuses')
        
class StatusAdmin(admin.ModelAdmin):
            list_display = ['rank' , 'name']
            
admin.site.register(Status, StatusAdmin)


class SGroup(models.Model):
    rank = models.SmallIntegerField(_('rank'))
    name = models.CharField(_('name'), max_length=30)
    member = models.ManyToManyField(User, verbose_name=_('member'))
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["rank"]
        verbose_name = _('support group')
        verbose_name_plural = _('support groups')
        
class SGroupAdmin(admin.ModelAdmin):
            list_display = ['rank' , 'name']
            
admin.site.register(SGroup, SGroupAdmin)

class UGroup(models.Model):
    rank = models.SmallIntegerField(_('rank'))
    name = models.CharField(_('name'), max_length=30)
    member = models.ManyToManyField(User, verbose_name=_('member'))
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["rank"]
        verbose_name = _('user group')
        verbose_name_plural = _('user groups')
        
class UGroupAdmin(admin.ModelAdmin):
            list_display = ['rank' , 'name']
            
admin.site.register(UGroup, UGroupAdmin)

class Ticket(models.Model):
    create_date = models.DateTimeField(_('create date'), auto_now_add=True)
    sgroup = models.ForeignKey(SGroup, verbose_name=_('support group'))
    priority = models.ForeignKey(Priority, verbose_name=_('priority'))
    status = models.ForeignKey(Status, verbose_name=_('status'))
#    suser = models.ForeignKey(User, verbose_name=_('support user'), unique=False)
    user = models.ForeignKey(User, verbose_name=_('user'), unique=True)
    category = models.ForeignKey(TicketCategory, verbose_name=_('category'))
    platform = models.ForeignKey(Platform, verbose_name=_('platform'))
    short = models.CharField(_('short description'), max_length=50)
    description = models.TextField(_('description'))
    last_update = models.DateTimeField(_('last update'), auto_now=True)
    class Meta:
        ordering = ["-create_date"]
        verbose_name = _('ticket')
        verbose_name_plural = _('tickets')
        
class TicketLog(models.Model):
    ticket = models.ForeignKey(Ticket, verbose_name=_('ticket'))
    user = models.ForeignKey(User, verbose_name=_('user'))
    initial = models.DateTimeField(_('initial date'))
    final = models.DateTimeField(_('final date'))
    action = models.CharField(_('action'), max_length=200)
    
class TicketLogInline(admin.TabularInline):
    model = TicketLog
    extra = 1
      
class TicketAdmin(admin.ModelAdmin):
    inlines = [TicketLogInline]
    
admin.site.register(Ticket, TicketAdmin)
